package edu.controller;

import edu.service.NoticeService;
import edu.service.UserService;
import edu.vo.Notice;
import edu.vo.User;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/edu")
public class MainController {
    @Autowired
    private UserService userService;

    @Autowired
    private NoticeService noticeService;

    @GetMapping("/main")
    public String home(Model model, HttpServletRequest request) {
        // 从Session中获取用户信息（假设登录时已存入）
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("loginUser");

        if (user != null) {
            // 将用户信息添加到模型中
            model.addAttribute("loginUser", user);
        } else {
            // 用户未登录，重定向到登录页
            return "redirect:/edu/login";
        }

        // 获取所有通知
        List<Notice> notices = noticeService.getAllNotices();
        model.addAttribute("notices", notices);

        // 设置活跃链接
        model.addAttribute("activeLink", "home");
        return "main";
    }
}
